-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathview.php
162 lines (144 loc) · 6.53 KB
/
view.php
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
<?php
/**
* @package mod_eductx
* @copyright 2021, Urban Vidovič <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
require('../../config.php');
require_once('lib.php');
require_once($CFG->dirroot . "/mod/eductx/classes/get_id_class.php");
require_once($CFG->dirroot . "/mod/eductx/classes/save_template_class.php");
require_once($CFG->dirroot . "/mod/eductx/classes/delete_template_class.php");
$id = optional_param('id', 0, PARAM_INT); // Course Module ID, or ...
$a = optional_param('a', 0, PARAM_INT); // eductx ID.
if ($id) {
if (!$cm = get_coursemodule_from_id('eductx', $id)) {
print_error('invalidcoursemodule');
}
if (!$course = $DB->get_record('course', array('id' => $cm->course))) {
print_error('coursemisconf');
}
} else {
if (!$eductx = $DB->get_record('eductx', array('id' => $a))) {
print_error('invalideductxid', 'eductx');
}
if (!$course = $DB->get_record('course', array('id' => $eductx->course))) {
print_error('invalidcourseid');
}
if (!$cm = get_coursemodule_from_instance("eductx", $eductx->id, $course->id)) {
print_error('invalidcoursemodule');
}
}
// Check login and get context.
require_login($course, false, $cm);
$context = context_module::instance($cm->id);
$PAGE->requires->js_call_amd('mod_eductx/main', 'init');
$PAGE->requires->js_call_amd('mod_eductx/buffer', 'init');
$PAGE->requires->js_call_amd('mod_eductx/jsonpack', 'init');
$PAGE->requires->js_call_amd('mod_eductx/eth_ecies', 'init');
$PAGE->requires->js_call_amd('mod_eductx/web3', 'init');
$PAGE->requires->js_call_amd('mod_eductx/eth_sig_util', 'init');
$PAGE->requires->js_call_amd('mod_eductx/pdfmake', 'init');
$PAGE->requires->js_call_amd('mod_eductx/qrcode', 'init');
$PAGE->requires->js_call_amd('mod_eductx/connector', 'init');
$getidform = new get_id_class();
$savetemplateform = new save_template_class();
$deletetemplateform = new delete_template_class();
$formdata = array("id" => $cm->id);
$getidform->set_data($formdata);
$savetemplateform->set_data($formdata);
$deletetemplateform->set_data($formdata);
$PAGE->set_url(new moodle_url('/mod/eductx/issue_certificateClass.php'));
$PAGE->set_context(context_system::instance());
$PAGE->set_title('EduCTX - Issue Certificate');
$PAGE->set_heading($course->fullname);
$roleassignments = $DB->get_records("role_assignments", ["userid" => $USER->id]);
$roles = array();
$isauthorized = false;
foreach ($roleassignments as $role) {
$rolefromid = $DB->get_record("role", ["id" => $role->roleid])->shortname;
// for the sake of one less DB query maybe change comparison from string to int? less robust?
if (is_siteadmin() || $rolefromid == "editingteacher" || $rolefromid == "coursecreator" || $rolefromid == "teacher" || $rolefromid == "manager") {
$isauthorized = true;
}
}
$didobj = $DB->get_record("did", ["userid" => $USER->id]);
$did = $didobj->did;
$recordid = $didobj->id;
$PAGE->requires->js_call_amd("mod_eductx/ui_driver", "sendIdToJs", [$did]);
$PAGE->requires->js_call_amd("mod_eductx/ui_driver", "sendUnitIdToJs", [$course->id]);
$PAGE->requires->js_call_amd("mod_eductx/ui_driver", "sendAuthorizedToJs", [$isauthorized]);
if ($fromform = $getidform->get_data()) {
$didfromform = $fromform->did;
$didobj = new stdClass();
$didobj->userid = $USER->id;
$didobj->did = $didfromform;
if ($did == NULL) {
$DB->insert_record("did", $didobj);
} else {
$didobj->id = $recordid;
$DB->update_record("did", (object)$didobj);
}
$PAGE->requires->js_call_amd("mod_eductx/ui_driver", "sendIdToJs", [$didfromform]);
$PAGE->requires->js_call_amd("mod_eductx/ui_driver", "updateErrorReporting",
["Linking successful", "Address <b>" . $fromform->address . "</b> has been linked to your account", "alert alert-success"]);
}
if ($fromform = $savetemplateform->get_data()) {
$templateobj = new stdClass();
$templateobj->teacherid = $USER->id;
$templateobj->name = $fromform->name;
$templateobj->title = $fromform->title;
$templateobj->achievement = $fromform->achievement;
$templateobj->wasawardedby = $fromform->wasAwardedBy;
$templateobj->grade = $fromform->grade;
$templateobj->ects = $fromform->ects;
$templateobj->awardingbodydescription = $fromform->awardingBodyDescription;
$templateobj->awardingdate = $fromform->awardingDate;
$templateobj->location = $fromform->location;
$templateobj->coursename = $fromform->courseName;
$DB->insert_record("templates", $templateobj);
$PAGE->requires->js_call_amd("mod_eductx/ui_driver", "updateErrorReporting",
["Template Saved", "Template <b>" . $fromform->name . "</b> has been saved.", "alert alert-success"]);
}
if ($fromform = $deletetemplateform->get_data()) {
$DB->delete_records("templates", ["id" => $fromform->deleteId]);
$PAGE->requires->js_call_amd("mod_eductx/ui_driver", "updateErrorReporting",
["Template Deleted", "Template <b>" . $fromform->deleteName . "</b> has been deleted.", "alert alert-success"]);
}
$role = "Regular User (Student)";
$issuerconfig = get_config("eductx");
$PAGE->requires->js_call_amd("mod_eductx/ui_driver", "sendIssuerEndpointToJs", [$issuerconfig->issuerendpoint]);
if ($isauthorized) {
$PAGE->requires->js_call_amd("mod_eductx/ui_driver", "sendApiKeyToJs", [$issuerconfig->apikey]);
// TEACHER'S FLOW
$role = "Certified Authority (Teacher)";
$students = get_role_users(5, context_course::instance($course->id));
$eligiblestudents = array();
$certtemplates = $DB->get_records("templates", ["teacherid" => $USER->id]);
foreach ($students as $student) {
$didobj = $DB->get_record("did", ["userid" => $student->id]);
$did = $didobj->did;
if ($did != NULL) {
$student->did = $did;
} else {
$student->did = NULL;
}
$eligiblestudents[] = $student;
}
}
echo $OUTPUT->header();
$templatecontext = (object)[
"addressPlaceholder" => "did:key:example",
"students" => array_values($eligiblestudents),
"courseId" => $course->id,
"eduCtxId" => $did,
"titleByRole" => $isauthorized ? "Connect wallet to issue Verifiable Credentials" : "Connect wallet to view claimable Verifiable Credentials",
"certTemplates" => array_values($certtemplates),
"role" => $role
];
echo $OUTPUT->render_from_template("eductx/home", $templatecontext);
$getidform->display();
$savetemplateform->display();
$deletetemplateform->display();
$PAGE->requires->js_call_amd('mod_eductx/ui_driver', 'initializeEventListeners');
echo $OUTPUT->footer();