lib.php 17 KB

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