Repository navigation
Expand file tree
/
Copy pathlib.php
More file actions
121 lines (105 loc) · 4.13 KB
/
Copy pathlib.php
File metadata and controls
121 lines (105 loc) · 4.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
<?php
/**
* Personality Test Library Functions
*
* @package block_personality_test
* @copyright 2026 SAVIO - Sistema de Aprendizaje Virtual Interactivo (UTB)
* @author SAVIO Development Team
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
require_once(dirname(__FILE__) . '/../../config.php');
function save_personality_test($course,$extra_res,$intra_res,$sensi_res,$intui_res,$ratio_res,$emoti_res,$estru_res,$perce_res, $responses = array()) {
GLOBAL $DB, $USER, $CFG;
// Check if user already has a personality test record (in any course)
$entry = $DB->get_record('personality_test', array('user' => $USER->id));
if (!$entry) {
$entry = new stdClass();
$entry->user = $USER->id;
$entry->course = $course;
$entry->is_completed = 1;
$entry->extraversion = $extra_res;
$entry->introversion = $intra_res;
$entry->sensing = $sensi_res;
$entry->intuition = $intui_res;
$entry->thinking = $ratio_res;
$entry->feeling = $emoti_res;
$entry->judging = $estru_res;
$entry->perceptive = $perce_res;
$entry->created_at = time();
$entry->updated_at = time();
// Add individual question responses
foreach ($responses as $field => $value) {
$entry->$field = $value;
}
$entry->id = $DB->insert_record('personality_test', $entry);
return true;
} else {
// Update existing record
$entry->is_completed = 1;
$entry->course = $course;
$entry->extraversion = $extra_res;
$entry->introversion = $intra_res;
$entry->sensing = $sensi_res;
$entry->intuition = $intui_res;
$entry->thinking = $ratio_res;
$entry->feeling = $emoti_res;
$entry->judging = $estru_res;
$entry->perceptive = $perce_res;
$entry->updated_at = time();
// Add individual question responses
foreach ($responses as $field => $value) {
$entry->$field = $value;
}
$DB->update_record('personality_test', $entry);
return true;
}
}
/**
* Helper function to get aggregated report data for a course.
*
* @param int $courseid The course ID.
* @return array|false Array containing [$course, $students] or false if permission denied.
*/
function block_personality_test_get_report_data($courseid) {
global $DB;
$course = $DB->get_record('course', array('id' => $courseid), '*', MUST_EXIST);
$context = context_course::instance($course->id);
require_login($course, false);
// Check permissions
if (!has_capability('block/personality_test:viewstudentdata', $context)) {
return false;
}
// Get enrolled students
$enrolled_students = get_enrolled_users($context, 'block/personality_test:taketest', 0, 'u.id');
$enrolled_ids = array_keys($enrolled_students);
// Filter out admins/teachers
$student_ids = array();
foreach ($enrolled_ids as $candidateid) {
$candidateid = (int)$candidateid;
if (has_capability('block/personality_test:viewstudentdata', $context, $candidateid)) {
continue;
}
$student_ids[] = $candidateid;
}
$students = array();
if (!empty($student_ids)) {
list($insql, $params) = $DB->get_in_or_equal($student_ids, SQL_PARAMS_NAMED);
$params['completed'] = 1;
$students = $DB->get_records_select('personality_test', "user $insql AND is_completed = :completed", $params);
}
return [$course, $students];
}
/**
* Helper function to calculate MBTI type string from test result.
*
* @param stdClass $entry The test result object.
* @return string The MBTI type string (e.g., "INTJ").
*/
function block_personality_test_calculate_mbti($entry) {
$mbti_score = "";
$mbti_score .= ($entry->extraversion > $entry->introversion) ? "E" : "I";
$mbti_score .= ($entry->sensing > $entry->intuition) ? "S" : "N";
$mbti_score .= ($entry->thinking >= $entry->feeling) ? "T" : "F";
$mbti_score .= ($entry->judging > $entry->perceptive) ? "J" : "P";
return $mbti_score;
}