lib.php 17 KB

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