lib.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509
  1. <?php
  2. /**
  3. * Library of functions and constants for module of activities iAssign.
  4. *
  5. * - v 1.3.1 2022/09/19
  6. * + Changes in comments (erased accents)
  7. * - v 1.3 2014/01/10
  8. * + Insert comment unread in course module (iassign_cm_info_view).
  9. * - v 1.2 2013/12/13
  10. * + Insert log in iAssign actions.
  11. *
  12. * @author Patricia Alves Rodrigues
  13. * @author Leônidas O. Brandão
  14. * @version v 1.3 2014/01/10
  15. * @package mod_iassign_lib
  16. * @since 2010/09/27
  17. * @copyright iMath (http://www.matematica.br) and LInE (http://line.ime.usp.br) - Computer Science Dep. of IME-USP (Brazil)
  18. *
  19. * <b>License</b>
  20. * - http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  21. */
  22. require_once($CFG->dirroot . "/mod/iassign/locallib.php");
  23. function iassign_extend_settings_navigation(settings_navigation $settingsnav, navigation_node $iassignnode) {
  24. global $USER, $PAGE, $CFG, $DB, $OUTPUT;
  25. if (optional_param('iassign_current', 0, PARAM_INT)) {
  26. $childnode = $iassignnode->create(get_string('edit_iassign', 'iassign'), new moodle_url('/mod/iassign/view.php', array('action' => 'edit', 'id' => optional_param('id', 0, PARAM_INT), 'iassign_current' => optional_param('iassign_current', 0, PARAM_INT) )), navigation_node::TYPE_SETTING);
  27. // 2019/02/12
  28. $children_key_list = $iassignnode->get_children_key_list();
  29. if (is_array($children_key_list) && count($children_key_list)>0)
  30. $iassignnode->add_node($childnode, $children_key_list[0]); //TODO: verify...
  31. // else $iassignnode->add_node($childnode, "");
  32. }
  33. }
  34. /// List of features supported in iAssign module
  35. // @param string $feature FEATURE_xx constant for requested feature
  36. // @return mixed True if module supports feature, false if not, null if doesn't know
  37. function iassign_supports ($feature) {
  38. switch ($feature) {
  39. case FEATURE_GROUPS: return false;
  40. case FEATURE_GROUPINGS: return false;
  41. case FEATURE_GROUPMEMBERSONLY: return false;
  42. case FEATURE_MOD_INTRO: return true;
  43. case FEATURE_SHOW_DESCRIPTION: return true;
  44. case FEATURE_COMPLETION_TRACKS_VIEWS: return true;
  45. case FEATURE_GRADE_HAS_GRADE: return true;
  46. case FEATURE_GRADE_OUTCOMES: return true;
  47. case FEATURE_GRADE_HAS_GRADE: return true;
  48. case FEATURE_BACKUP_MOODLE2: return true;
  49. default: return null;
  50. }
  51. }
  52. /// Function for insert a link with messages in iassigns for students and teacher.
  53. // @param cm_info $cm_info array Informations of module course.
  54. // @see http://docs.moodle.org/dev/Module_visibility_and_display
  55. function iassign_cm_info_view (cm_info $cm_info) {
  56. global $CFG, $DB, $USER, $COURSE;
  57. $comment_unread = "";
  58. $sum_comment = 0;
  59. $iassign_statements = $DB->get_records("iassign_statement", array("iassignid" => $cm_info->instance));
  60. foreach ($iassign_statements as $iassign_statement) {
  61. $cm = get_coursemodule_from_instance("iassign", $iassign_statement->id, optional_param('id', 0, PARAM_INT));
  62. if ($cm) {
  63. $contextuser = context_module::instance($cm->id);
  64. $teacher_access = has_capability('mod/iassign:evaluateiassign', $contextuser, $USER->id);
  65. $student_access = has_capability('mod/iassign:submitiassign', $contextuser, $USER->id);
  66. $iassign_submissions = array();
  67. if ($teacher_access) {
  68. $receiver = '1';
  69. $iassign_submissions = $DB->get_records("iassign_submission", array("iassign_statementid" => $iassign_statement->id));
  70. }
  71. else
  72. if ($student_access) {
  73. $receiver = '2';
  74. $iassign_submissions = $DB->get_records("iassign_submission", array("iassign_statementid" => $iassign_statement->id, "userid" => $USER->id));
  75. }
  76. foreach ($iassign_submissions as $iassign_submission) {
  77. $params = array('iassign_submissionid' => $iassign_submission->id, 'return_status' => '0', 'receiver' => $receiver);
  78. $strQuery = 'SELECT COUNT(iassign_submissionid) FROM {iassign_submission_comment} WHERE ' .
  79. ' iassign_submissionid = :iassign_submissionid AND return_status= :return_status AND receiver= :receiver';
  80. $verify_message = $DB->get_record_sql($strQuery, $params);
  81. if ($verify_message)
  82. foreach ($verify_message as $tmp)
  83. $sum_comment += $tmp;
  84. }
  85. }
  86. }
  87. if ($sum_comment != 0) {
  88. $comment_unread_message = get_string('comment_unread', 'iassign');
  89. if ($sum_comment == 1)
  90. $comment_unread_message = get_string('comment_unread_one', 'iassign');
  91. if ($teacher_access)
  92. $comment_unread = "&nbsp;&nbsp;<a href='" . $CFG->wwwroot . "/mod/iassign/view.php?id=" . $cm_info->id . "&action=report&iassignid=" . $cm_info->instance . "'><font color='red'>" . iassign_icons::insert('comment_unread') . "&nbsp;($sum_comment&nbsp;" . $comment_unread_message . ")</font></a>";
  93. else if ($student_access) //http://localhost/moodle24/mod/iassign/view.php?id=463&userid_iassign=4&action=view&iassign_current=1
  94. $comment_unread = "&nbsp;&nbsp;<font color='red'>" . iassign_icons::insert('comment_unread') . "&nbsp;($sum_comment&nbsp;" . get_string('comment_unread', 'iassign') . ")</font>";
  95. }
  96. $cm_info->set_after_link($comment_unread);
  97. }
  98. /// This function is used by the reset_course_userdata function in moodlelib.
  99. // @param $data the data submitted from the reset course.
  100. // @return array status array
  101. function iassign_reset_userdata ($data) {
  102. return array();
  103. }
  104. /// List of view style log actions
  105. // @return array
  106. function iassign_get_view_actions () {
  107. return array('view', 'view submission');
  108. }
  109. /// List of update style log actions
  110. // @return array
  111. function iassign_get_post_actions () {
  112. return array('update', 'add', 'upload', 'update comment', 'update submission', 'delete iassign', 'add comment', 'add submission');
  113. }
  114. /// Adds an iAssign instance
  115. // @param object $iassign An object from the form in mod_form.php
  116. // @return Fail / id number of the new instance
  117. function iassign_add_instance ($data, $mform) {
  118. global $DB;
  119. $cmid = $data->coursemodule;
  120. $iassignid = $DB->insert_record("iassign", $data);
  121. $data->id = $iassignid;
  122. $context = context_module::instance($cmid);
  123. $iassign = $DB->get_record('iassign', array('id' => $iassignid), '*', MUST_EXIST);
  124. iassign_grade_item($iassign);
  125. // log event -----------------------------------------------------
  126. iassign_log::add_log('add_iassign', 'name: ' . $data->name, $cmid);
  127. // log event -----------------------------------------------------
  128. return $iassign->id;
  129. }
  130. /// Display an item in grade of activities iAssign.
  131. // @param object $iassign An object from the form.
  132. function iassign_grade_item ($iassign) {
  133. global $DB, $CFG;
  134. require_once($CFG->libdir . '/gradelib.php');
  135. /// @todo Ver código comentado
  136. // if (!$iassign->id = $DB->insert_record("iassign", $iassign))
  137. // return false;
  138. // $iassign=(stripslashes_recursive($iassign));
  139. $iassign->grade = 0;
  140. $grades = NULL;
  141. if (array_key_exists('cmidnumber', $iassign)) { //it may not be always present
  142. $params = array('itemname' => $iassign->name, 'idnumber' => $iassign->cmidnumber);
  143. }
  144. else {
  145. $params = array('itemname' => $iassign->name);
  146. }
  147. if ($iassign->grade > 0) {
  148. $params['gradetype'] = GRADE_TYPE_VALUE;
  149. $params['gradiLMx'] = $iassign->grade;
  150. $params['grademin'] = 0;
  151. }
  152. else {
  153. $params['gradetype'] = GRADE_TYPE_NONE;
  154. }
  155. if ($grades === 'reset') {
  156. $params['reset'] = true;
  157. $grades = NULL;
  158. }
  159. else if (!empty($grades)) {
  160. // Need to calculate raw grade (Note: $grades has many forms)
  161. if (is_object($grades)) {
  162. $grades = array($grades->userid => $grades);
  163. }
  164. else if (array_key_exists('userid', $grades)) {
  165. $grades = array($grades['userid'] => $grades);
  166. }
  167. foreach ($grades as $key => $grade) {
  168. $grades[$key] = $grade = (array) $grade;
  169. }
  170. $grades[$key]['rawgrade'] = ($grade['rawgrade'] * $iassign->grade / 100);
  171. }
  172. grade_update('mod/iassign', $iassign->course, 'mod', 'iassign', $iassign->id, 0, $grades, $params);
  173. }
  174. /// This function will update an existing instance with new data.
  175. // @param object $iassign An object from the form in mod.html
  176. // @return boolean Fail Return the result.
  177. function iassign_update_instance ($data, $mform) {
  178. global $DB, $CFG;
  179. $data->id = $data->instance;
  180. $cmid = $data->coursemodule;
  181. $DB->update_record("iassign", $data);
  182. $context = context_module::instance($cmid);
  183. iassign_grade_item_update($data->id);
  184. // log event -----------------------------------------------------
  185. iassign_log::add_log('update_iassign', 'name: ' . $data->name, $cmid);
  186. // log event -----------------------------------------------------
  187. return true;
  188. }
  189. /// Update an item in grade of activities iAssign.
  190. // @param int $iassignid Id to activities iAssign
  191. function iassign_grade_item_update ($iassignid) {
  192. global $USER, $CFG, $COURSE, $DB, $OUTPUT;
  193. require_once($CFG->libdir . '/gradelib.php');
  194. // $sum_grade = $DB->get_records_sql("SELECT SUM(grade) as total FROM {$CFG->prefix}iassign_statement s WHERE s.iassignid = '$iassignid' AND s.type_iassign=3");
  195. $sum_grade = 0;
  196. $grade = $DB->get_records('iassign_statement', array('iassignid' => $iassignid, 'type_iassign' => 3));
  197. foreach ($grade as $tmp) {
  198. $sum_grade += $tmp->grade;
  199. }
  200. $grade_iassign = $DB->get_record("iassign", array("id" => $iassignid));
  201. $grades = NULL;
  202. $params = array('itemname' => $grade_iassign->name);
  203. $params['iteminstance'] = $iassignid;
  204. $params['gradetype'] = GRADE_TYPE_VALUE;
  205. if ($sum_grade != 0) {
  206. $params['gradiLMx'] = $sum_grade;
  207. $params['rawgradiLMx'] = $sum_grade;
  208. }
  209. else {
  210. $params['gradiLMx'] = 0;
  211. $params['rawgradiLMx'] = 0;
  212. }
  213. $params['grademin'] = 0;
  214. grade_update('mod/iassign', $grade_iassign->course, 'mod', 'iassign', $iassignid, 0, $grades, $params);
  215. $grades = $DB->get_records('grade_grades', array('itemid' => $iassignid));
  216. if ($grades) {
  217. foreach ($grades as $grade) {
  218. $grade->rawgradiLMx = $params['rawgradiLMx'];
  219. $DB->update_record("grade_grades", $grade);
  220. }
  221. }
  222. }
  223. /// Delete an item in grade of activities iAssign.
  224. // @param int $id Id to activities iAssign
  225. function iassign_delete_instance ($id) {
  226. global $DB;
  227. $result = true;
  228. if (!$iassign = $DB->get_record("iassign", array("id" => $id))) {
  229. return false;
  230. }
  231. $DB->delete_records('event', array('modulename' => 'iassign', 'instance' => $iassign->id));
  232. iassign_grade_item_delete($iassign);
  233. $iassign_statements = $DB->get_records("iassign_statement", array("iassignid" => $iassign->id));
  234. if ($iassign_statements) {
  235. foreach ($iassign_statements as $iassign_statement) {
  236. $iassign_statements_submissions = $DB->get_records("iassign_submission", array("iassign_statementid" => $iassign_statement->id));
  237. if ($iassign_statements_submissions) {
  238. foreach ($iassign_statements_submissions as $iassign_statements_submission) {
  239. $DB->delete_records('iassign_submission_comment', array('iassign_submissionid' => $iassign_statements_submission->id));
  240. }
  241. $DB->delete_records("iassign_submission ", array("iassign_statementid" => $iassign_statement->id));
  242. }
  243. }
  244. $DB->delete_records("iassign_statement", array("iassignid" => $iassign->id));
  245. }
  246. if (!$DB->delete_records("iassign", array("id" => $iassign->id))) {
  247. $result = false;
  248. }
  249. return $result;
  250. }
  251. /// Given an ID of an instance of this module,
  252. // this function will permanently delete the instance
  253. // and any data that depends on it.
  254. // @param object $iassign
  255. function iassign_grade_item_delete ($iassign) {
  256. global $DB, $CFG;
  257. require_once($CFG->libdir . '/gradelib.php');
  258. grade_update('mod/iassign', $iassign->course, 'mod', 'iassign', $iassign->id, 0, NULL, array('deleted' => 1));
  259. }
  260. /// Return a small object with summary information about what a
  261. // user has done with a given particular instance of this module
  262. // Used for user activity reports.
  263. // $return->time = the time they did it
  264. // $return->info = a short text description
  265. // @return boolean Return the state of function.
  266. function iassign_user_outline ($course, $user, $mod, $iassign) {
  267. return true;
  268. }
  269. /// Print a detailed representation of what a user has done with
  270. // a given particular instance of this module, for user activity reports.
  271. // @return boolean Return the state of function.
  272. function iassign_user_complete ($course, $user, $mod, $iassign) {
  273. return true;
  274. }
  275. /// Given a course and a time, this module should find recent activity
  276. // that has occurred in ia activities and print it out.
  277. // Return true if there was output, or false is there was none.
  278. // @return boolean Return the state of function.
  279. function iassign_print_recent_activity ($course, $isteacher, $timestart) {
  280. global $DB, $CFG;
  281. return false; // True if anything was printed, otherwise false
  282. }
  283. /// Function to be run periodically according to the moodle cron
  284. // This function searches for things that need to be done, such
  285. // as sending out mail, toggling flags etc ...
  286. // @return boolean Return the state of function.
  287. function iassign_cron () {
  288. global $DB, $CFG;
  289. return true;
  290. }
  291. /// Must return an array of grades for a given instance of this module,
  292. // indexed by user. It also returns a maximum allowed grade.
  293. // Example:
  294. // $return->grades = array of grades;
  295. // $return->maxgrade = maximum allowed grade;
  296. // return $return;
  297. // @param int $iassignid ID of an instance of this module
  298. // @return mixed Null or object with an array of grades and with the maximum grade
  299. function iassign_grades ($iassignid) {
  300. return NULL;
  301. }
  302. /// Must return an array of user records (all data) who are participants
  303. // for a given instance of ia. Must include every user involved
  304. // in the instance, independient of his role (student, teacher, admin...)
  305. // See other modules as example.
  306. // @param int $iassignid ID of an instance of this module
  307. // @return mixed boolean/array of students
  308. function iassign_get_participants ($iassignid) {
  309. return false;
  310. }
  311. /// This function returns if a scale is being used by one ia
  312. // it it has support for grading and scales. Commented code should be
  313. // modified if necessary. See forum, glossary or journal modules
  314. // as reference.
  315. // @param int $iassignid ID of an instance of this module
  316. // @return mixed boolean/array of students
  317. function iassign_scale_used ($iassignid, $scaleid) {
  318. $return = false;
  319. /// @todo Ver código comentado
  320. //$rec = $DB->get_record("iassign","id","$iassignid","scale","-$scaleid");
  321. //if (!empty($rec) && !empty($scaleid)) { $return = true; }
  322. return $return;
  323. }
  324. /// Checks if scale is being used by any instance of ia.
  325. // This function was added in 1.9
  326. // This is used to find out if scale used anywhere
  327. // @param $scaleid int
  328. // @return boolean True if the scale is used by any ia
  329. function iassign_scale_used_anywhere ($scaleid) {
  330. if ($scaleid and record_exists('iassign', 'grade', -$scaleid)) {
  331. return true;
  332. }
  333. else {
  334. return false;
  335. }
  336. }
  337. /// Search iLM
  338. // @param boolean $enable
  339. // @return array of iLM
  340. function search_iLM ($enable) {
  341. global $DB;
  342. $ilms = $DB->get_records('iassign_ilm', array('enable' => $enable));
  343. return $ilms;
  344. }
  345. /// Base implementation for backing up subtype specific information
  346. // for one single module
  347. // @return boolean Return the state of function.
  348. function backup_one_mod ($bf, $preferences, $iassign) {
  349. return true;
  350. }
  351. /// Base implementation for backing up subtype specific information
  352. // for one single submission
  353. // @return boolean Return the state of function.
  354. function backup_one_submission ($bf, $preferences, $iassign, $submission) {
  355. return true;
  356. }
  357. /// Base implementation for restoring subtype specific information
  358. // for one single module
  359. // @return boolean Return the state of function.
  360. function restore_one_mod ($info, $restore, $iassign) {
  361. return true;
  362. }
  363. /// Base implementation for restoring subtype specific information
  364. // for one single submission
  365. // @return boolean
  366. function restore_one_submission ($info, $restore, $iassign, $submission) {
  367. return true;
  368. }
  369. /// Serves the data attachments. Implements needed access control ;-)
  370. // @param object $course
  371. // @param object $cm
  372. // @param object $context
  373. // @param string $filearea
  374. // @param array $args
  375. // @param bool $forcedownload
  376. // @return bool false if file not found, does not return if found - justsend the file
  377. function iassign_pluginfile ($course, $cm, $context, $filearea, $args, $forcedownload) {
  378. global $CFG, $DB;
  379. require_course_login($course, true, $cm);
  380. $fileareas = array('exercise', 'submit', 'activity', 'ilm');
  381. if (!in_array($filearea, $fileareas)) {
  382. return false;
  383. }
  384. $fs = get_file_storage();
  385. $postid = (int) array_shift($args);
  386. $relativepath = implode('/', $args);
  387. $fullpath = "/$context->id/mod_iassign/$filearea/$postid/$relativepath";
  388. if (!$file = $fs->get_file_by_hash(sha1($fullpath)) or $file->is_directory()) {
  389. return false;
  390. }
  391. // finally send the file
  392. send_stored_file($file, 0, 0, false); // download MUST be forced - security!
  393. return false;
  394. }
  395. /// Display an url from access iLM.
  396. // @calledby settings.php
  397. // @param string $url Initial url
  398. // @return string Return the url formatted.
  399. function display_url_ilm ($url) {
  400. // note: empty URL are prevented in form validation
  401. $url = trim($url);
  402. // remove encoded entities - we want the raw URI here
  403. $url = html_entity_decode($url, ENT_QUOTES, 'UTF-8');
  404. if (!preg_match('|^[a-z]+:|i', $url) and ! preg_match('|^/|', $url)) {
  405. // invalid URI, try to fix it by making it normal URL, please note relative url are not allowed, /xx/yy links are ok
  406. $url = 'http://' . $url;
  407. }
  408. return $url;
  409. }