lib.php 17 KB

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