From 1e41f659291fcea976c2d939e5b6aac02a848b05 Mon Sep 17 00:00:00 2001 From: Myriam Delaruelle <myriam.delaruelle@univ-lorraine.fr> Date: Mon, 9 May 2022 11:56:01 +0200 Subject: [PATCH] delete/add an activity to the matrix if it's deleted/duplicated --- block_competency_iena.php | 183 +++++++++++++++++++------------------- classes/observer.php | 21 +++++ db/events.php | 15 ++++ lib.php | 32 +++++++ 4 files changed, 160 insertions(+), 91 deletions(-) create mode 100644 classes/observer.php create mode 100644 db/events.php create mode 100644 lib.php diff --git a/block_competency_iena.php b/block_competency_iena.php index ffbc814..c6ab020 100644 --- a/block_competency_iena.php +++ b/block_competency_iena.php @@ -1,98 +1,99 @@ <?php -require_once('entity/block_competency_iena_competency.php'); +defined('MOODLE_INTERNAL') || die(); -class block_competency_iena extends block_base -{ - public function init() - { - $this->title = get_string('title_plugin', 'block_competency_iena'); - } +class block_competency_iena extends block_base{ + public function init() + { + $this->title = get_string('title_plugin', 'block_competency_iena'); + } - function instance_allow_multiple() - { - return false; - } + function instance_allow_multiple() + { + return false; + } - /** - * Set the applicable formats for this block to all - * @return array - */ - function applicable_formats() - { - return array('course' => true); - } - - /** - * Allow the user to configure a block instance - * @return bool Returns true - */ - function instance_allow_config() - { - return true; - } - - function has_config() - { - return true; - } - - public function get_content() - { - global $CFG; - global $COURSE; - global $USER; - - if ($this->content !== null) { - return $this->content; - } - if (empty($this->config)) { - $this->config = new stdClass(); - } - - $this->content = new stdClass; - $this->content->text = ""; - if (has_capability('moodle/course:update', $context = context_course::instance($COURSE->id), $USER->id)) { - $this->content->text .= '<h4>Tableaux de bord</h4>'; - $this->content->text .= '<a href="' . $CFG->wwwroot . '/blocks/competency_iena/competency_iena_competencies_2.php?courseid=' . $COURSE->id . '" class="btn btn-primary w-100 mb-3" style="white-space: normal;">Par étudiant</a>'; - $this->content->text .= '<a href="' . $CFG->wwwroot . '/blocks/competency_iena/competency_iena_competency_students_2.php?courseid=' . $COURSE->id . '" class="btn btn-primary w-100" style="white-space: normal;">Par compétence</a>'; - // marche plus - // $this->content->text .= '<a href="' . $CFG->wwwroot . '/blocks/competency_iena/competency_iena_competencies.php?courseid=' . $COURSE->id . '" class="btn btn-primary w-100 mb-3" style="white-space: normal;">Acquisition des compétences</a>'; - $this->content->text .= '<hr>'; - $this->content->text .= '<a href="' . $CFG->wwwroot . '/blocks/competency_iena/competency_iena_competencies_mgmt.php?courseid=' . $COURSE->id . '" type="button " class="btn btn-secondary w-100 mb-3" style="white-space: normal;">Gérer les compétences</a>'; - $this->content->text .= '<a href="' . $CFG->wwwroot . '/blocks/competency_iena/competency_iena_competency_mgmt.php?courseid=' . $COURSE->id . '" type="button " class="btn btn-secondary w-100 mb-3" style="white-space: normal;">Informations sur l\'APC</a>'; - } else { - $this->content->text .= '<a href="' . $CFG->wwwroot . '/blocks/competency_iena/competency_iena_competencies_2.php?courseid=' . $COURSE->id . '" class="btn btn-primary w-100 mb-3" style="white-space: normal;">Mes compétences</a>'; - } + /** + * Set the applicable formats for this block to all + * @return array + */ + function applicable_formats() + { + return array('course' => true); + } + + /** + * Allow the user to configure a block instance + * @return bool Returns true + */ + function instance_allow_config() + { + return true; + } + + function has_config() + { + return true; + } + + public function get_content() + { + global $CFG; + global $COURSE; + global $USER; + + if ($this->content !== null) { + return $this->content; + } + if (empty($this->config)) { + $this->config = new stdClass(); + } + + $this->content = new stdClass; + $this->content->text = ""; + if (has_capability('moodle/course:update', $context = context_course::instance($COURSE->id), $USER->id)) { + $this->content->text .= '<h4>Tableaux de bord</h4>'; + $this->content->text .= '<a href="' . $CFG->wwwroot . '/blocks/competency_iena/competency_iena_competencies_2.php?courseid=' . $COURSE->id . '" class="btn btn-primary w-100 mb-3" style="white-space: normal;">Par étudiant</a>'; + $this->content->text .= '<a href="' . $CFG->wwwroot . '/blocks/competency_iena/competency_iena_competency_students_2.php?courseid=' . $COURSE->id . '" class="btn btn-primary w-100" style="white-space: normal;">Par compétence</a>'; + // marche plus + // $this->content->text .= '<a href="' . $CFG->wwwroot . '/blocks/competency_iena/competency_iena_competencies.php?courseid=' . $COURSE->id . '" class="btn btn-primary w-100 mb-3" style="white-space: normal;">Acquisition des compétences</a>'; + $this->content->text .= '<hr>'; + $this->content->text .= '<a href="' . $CFG->wwwroot . '/blocks/competency_iena/competency_iena_competencies_mgmt.php?courseid=' . $COURSE->id . '" type="button " class="btn btn-secondary w-100 mb-3" style="white-space: normal;">Gérer les compétences</a>'; + $this->content->text .= '<a href="' . $CFG->wwwroot . '/blocks/competency_iena/competency_iena_competency_mgmt.php?courseid=' . $COURSE->id . '" type="button " class="btn btn-secondary w-100 mb-3" style="white-space: normal;">Informations sur l\'APC</a>'; + } else { + $this->content->text .= '<a href="' . $CFG->wwwroot . '/blocks/competency_iena/competency_iena_competencies_2.php?courseid=' . $COURSE->id . '" class="btn btn-primary w-100 mb-3" style="white-space: normal;">Mes compétences</a>'; + } - // $competenceI = new block_competency_iena_competency(); - // $competences = $competenceI->get_competencies_by_userID($USER->id); - // $nb_ok = 0; - // $nb_total = count($competences); - // foreach ($competences as $comp) { - // if ($comp->proficiency == 1) { - // $nb_ok++; - // } - // } - // // $this->content->text .= "<p></p>"; - // if ($nb_total > 0) { - // $progress_percentage = ceil($nb_ok*100/$nb_total); - // $this->content->text .= " - // <div class=\"progress\"> - // <div class=\"progress-bar\" role=\"progressbar\" style=\"width: $progress_percentage%;\" aria-valuenow=\"$nb_ok\" aria-valuemin=\"0\" aria-valuemax=\"$nb_total\">$nb_ok/$nb_total</div> - // </div> - // "; - // } else { - // $progress_percentage = 0; - // $this->content->text .= " - // <div class=\"progress\"> - // <div class=\"progress-bar\" role=\"progressbar\" style=\"width: $progress_percentage%;\" aria-valuenow=\"$nb_ok\" aria-valuemin=\"0\" aria-valuemax=\"$nb_total\"></div> - // </div> - // "; - // } - - return $this->content; - } - } + // $competenceI = new block_competency_iena_competency(); + // $competences = $competenceI->get_competencies_by_userID($USER->id); + // $nb_ok = 0; + // $nb_total = count($competences); + // foreach ($competences as $comp) { + // if ($comp->proficiency == 1) { + // $nb_ok++; + // } + // } + // // $this->content->text .= "<p></p>"; + // if ($nb_total > 0) { + // $progress_percentage = ceil($nb_ok*100/$nb_total); + // $this->content->text .= " + // <div class=\"progress\"> + // <div class=\"progress-bar\" role=\"progressbar\" style=\"width: $progress_percentage%;\" aria-valuenow=\"$nb_ok\" aria-valuemin=\"0\" aria-valuemax=\"$nb_total\">$nb_ok/$nb_total</div> + // </div> + // "; + // } else { + // $progress_percentage = 0; + // $this->content->text .= " + // <div class=\"progress\"> + // <div class=\"progress-bar\" role=\"progressbar\" style=\"width: $progress_percentage%;\" aria-valuenow=\"$nb_ok\" aria-valuemin=\"0\" aria-valuemax=\"$nb_total\"></div> + // </div> + // "; + // } + + return $this->content; + } - ?> \ No newline at end of file + + +} +?> \ No newline at end of file diff --git a/classes/observer.php b/classes/observer.php new file mode 100644 index 0000000..359dc70 --- /dev/null +++ b/classes/observer.php @@ -0,0 +1,21 @@ +<?php +defined('MOODLE_INTERNAL') || die(); +//require_once($CFG->dirroot.'/blocks/competency_iena/block_competency_iena.php'); +require_once($CFG->dirroot.'/blocks/competency_iena/lib.php'); + +class block_competency_iena_observer { + /** + * Observer that monitors course module suppressed event and update the linked matrix competencies . + * + * @param \core\event\course_module_deleted $event the event object. + */ + public static function course_module_deleted(\core\event\course_module_deleted $event) { + $cmid = $event->contextinstanceid; + delete_matrix_competencies($cmid); + } + + public static function course_module_created(\core\event\course_module_created $event) { + $cmid = $event->contextinstanceid; + duplicate_matrix_competencies($cmid, ); + } +} diff --git a/db/events.php b/db/events.php new file mode 100644 index 0000000..434eba2 --- /dev/null +++ b/db/events.php @@ -0,0 +1,15 @@ +<?php +defined('MOODLE_INTERNAL') || die(); + +$observers = array( + array( + 'eventname' => '\core\event\course_module_deleted', + 'callback' => 'block_competency_iena_observer::course_module_deleted', + ), + array( + 'eventname' => '\core\event\course_module_created', + 'callback' => 'block_competency_iena_observer::course_module_created', + ) +); + +?> \ No newline at end of file diff --git a/lib.php b/lib.php new file mode 100644 index 0000000..fe13a6e --- /dev/null +++ b/lib.php @@ -0,0 +1,32 @@ +<?php + +defined('MOODLE_INTERNAL') || die(); + +/** + * Delete an activity from the matrix when it's deleted from a course + * @param int $moduleid + */ +function delete_matrix_competencies($moduleid){ + global $DB; + $activity=$DB->record_exists('block_competency_iena', array('cmid' => $moduleid)); + if($activity){ + $DB->delete_records('block_competency_iena', array('cmid' => $moduleid)); + } + + +} + +/** + * Add an activity to the matrix when it's duplicated (if the new one is linked to competencies). Moodle automatically add the corresponding competencies to the new activity, so we just have to check that to add it to the matrix if necessary + * @param int $moduleid + */ +function duplicate_matrix_competencies($moduleid){ + global $DB; + $sql = "SELECT mc.cmid, m.section FROM {competency_modulecomp} mc JOIN {course_modules} m ON mc.cmid=m.id WHERE mc.cmid= :moduleid"; + $params=array('moduleid'=>$moduleid); + $activity=$DB->get_record_sql($sql, $params); + if($activity){ + $DB->insert_record('block_competency_iena', array('cmid' => $activity->cmid, 'sectionid'=>$activity->section)); + } + +} \ No newline at end of file -- GitLab